<template>
  <div style="height: 20vh;background-color:#fff;padding:10px 20px;">
    <el-card style="height: 130px;">
      <el-form :inline="true" >
        <el-form-item label="停车场编号:">
          <el-input placeholder="请输入停车场编号" style="width:190px;"
                    v-model="searchParkingForm.parkcode" @keydown.enter="loadParking"></el-input>
        </el-form-item>
        <el-form-item label="停车场名称:">
          <el-input placeholder="请输入停车场名称" style="width:190px;"
                    v-model="searchParkingForm.name" @keydown.enter="loadParking"></el-input>
        </el-form-item>
        <el-form-item label="地址:">
          <el-input placeholder="请输入停车场地址" style="width:190px;"
                    v-model="searchParkingForm.address" @keydown.enter="loadParking"></el-input>
        </el-form-item>
        <el-form-item label="负责人:">
          <el-select v-model="searchParkingForm.adminId" placeholder="请选择负责人" style="width: 190px">
            <el-option label="tom" value="1"></el-option>
            <el-option label="李四" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="searchParkingForm.status" placeholder="请选择停车场状态" style="width: 190px">
            <el-option label="正常" value="1"></el-option>
            <el-option label="拥挤" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" >
        <el-form-item>
          <el-button @click="beforeAddParking" type="success">新建</el-button>
          <el-button type="danger" @click="resetSearch">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadParking">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

  </div>
  <!-- 弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="停车场编号">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.parkcode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停车场名称">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停车场地址">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.address"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="负责人">
            <el-select v-model="saveParkingForm.adminId" placeholder="请选择负责人">
              <el-option label="tom" value="1"></el-option>
              <el-option label="李四" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
<!--        <el-col :span="12">
          <el-form-item label="联系方式">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.phone"></el-input>
          </el-form-item>
        </el-col>-->
      </el-row>
      <el-row :gutter="30">
<!--        <el-col :span="12">
          <el-form-item label="车库数">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.code"></el-input>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="车位数">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.parkingSpace"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
<!--        <el-col :span="12">
          <el-form-item label="计费规则">
            <el-input placeholder="请输入内容" v-model="saveParkingForm.chargeRuleName"></el-input>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="状态">
            <el-select placeholder="请选择" v-model="saveParkingForm.status">
              <el-option label="正常" value="1"></el-option>
              <el-option label="拥挤" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveParking">确定</el-button>
    </template>
  </el-dialog>

  <!-- 停车场列表 -->
  <el-card style="margin:20px;">
    <el-table :data="parkingArr" >
      <el-table-column type="index" label="停车场ID" width="80" align="center"/>
      <el-table-column prop="parkcode" label="编号" align="center"/>
      <el-table-column prop="name" label="停车场名称" align="center"/>
      <el-table-column prop="address" label="停车场地址" align="center"/>
      <el-table-column prop="parkingSpace" label="车位数" align="center"/>
      <el-table-column prop="occupySpace" label="占用车位数" align="center"/>
      <el-table-column prop="adminId" label="负责人" align="center"/>
      <el-table-column prop="status" label="状态" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"
                     @click="editParking(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteParking(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";


//控制新增停车场弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增停车场');

//保存停车场搜索条件
const searchParkingForm = ref({parkcode:'',name:'',address:'',adminId:'',status:''});
//保存停车场表格数据
const parkingArr = ref([]);
//加载停车场的方法
const loadParking = ()=>{
  let data = qs.stringify(searchParkingForm.value);
  axios.get(BASE_URL+'/parklot/select?'+data).then((response)=>{
    if(response.data.code==2000){
      parkingArr.value = response.data.data;
      parkingArr.value.forEach(item=>{
        item.status = item.status==1?'正常':'拥挤';
        item.adminId = item.adminId==1?'tom':'李四';
      })
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//页面打开加载停车场方法
onMounted(()=>{
  loadParking();
})
const resetSearch = ()=>{
  searchParkingForm.value = {};
  loadParking();
}

//定义对象用来收集停车场弹窗表单的所有数据
const saveParkingForm = ref({
  parkcode:'',
  name:'',
  address:'',
  adminId:'',
  phone:'',
  code:'',
  parkingSpace:'',
  chargeRuleName:'',
  status:'',
  createTime:'',
  updateTime:''
})
//点击"新建停车场"按钮,调用此函数
const beforeAddParking = ()=>{
  //确保每次新增时的标题,且弹窗表单中没有数据
  dialogTitle.value = '新增停车场';
  dialogVisible.value = true;
  saveParkingForm.value = {};
}

//定义保存的方法
const saveParking = ()=>{
  //把收集到的表单数据转为查询字符串格式
  let data = qs.stringify(saveParkingForm.value);
  //给后端发请求
  axios.post(BASE_URL+'/parklot/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('保存成功!');
      saveParkingForm.value = {};
      dialogVisible.value = false;
      loadParking();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

const handleClose = ()=>{
  if(confirm('您确认要关闭本弹窗吗?')){
    dialogVisible.value = false;
  }
}

//查看
const showParking = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '停车场信息';
}
//编辑
const editParking = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑停车场';
  axios.get(BASE_URL+'/parklot/select?id='+id).then((response)=>{
    if(response.data.code===2000){
      parkingArr.value.forEach(item=>{
        item.status = item.status==1?'正常':'拥挤';
        item.adminId = item.adminId==1?'tom':'李四';
      })
      saveParkingForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//删除
const deleteParking = (id)=>{
  if(confirm('您确认要删除此停车场吗?')){
    axios.post(BASE_URL+'/parklot/delete/'+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功!');
        loadParking();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>